package com.tan.steamship.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tan.steamship.common.Result;
import com.tan.steamship.domain.MessagePo;
import com.tan.steamship.domain.UserPo;
import com.tan.steamship.dto.MessageDto;
import com.tan.steamship.mapper.MessageMapper;
import com.tan.steamship.service.MessageService;
import com.tan.steamship.service.UserService;
import com.tan.steamship.util.SendEmailUtil;
import com.tan.steamship.vo.MessageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 85726
* @description 针对表【message(消息表)】的数据库操作Service实现
* @createDate 2025-03-31 18:00:38
*/
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, MessagePo>
    implements MessageService{

    @Autowired
    private UserService userService;
    @Autowired
    SendEmailUtil sendEmailUtil;

    @Override
    public Page<MessageVo> listAll(Integer pageIndex,Integer pageSize) {
        Page<MessageVo> result = new Page<>();
        LambdaQueryWrapper<MessagePo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(MessagePo::getCreateTime);
        Page<MessagePo> page = page(new Page<>(pageIndex, pageSize), queryWrapper);
        List<MessagePo> messagePos = page.getRecords();
        List<MessageVo> listVo = new LinkedList<>();
        if (CollUtil.isNotEmpty(messagePos)) {
            listVo = BeanUtil.copyToList(messagePos, MessageVo.class, new CopyOptions());
        }
        result.setTotal(page.getTotal());
        result.setCurrent(page.getCurrent());
        result.setRecords(listVo);
        return result;
    }

    @Override
    public Result saveOrUpdate(MessageDto messageDto) {
        Result result = new Result();
        if (StrUtil.isNotBlank(messageDto.getSendStatue()) && !"未发送".equals(messageDto.getSendStatue())) {
            result.setCode(1001);
            result.setMessage("只能修改未发送状态的数据");
        }
        messageDto.setSendStatue("未发送");
        messageDto.setCreateBy("admin");
        MessagePo messagePo = BeanUtil.copyProperties(messageDto, MessagePo.class);
        if (ObjectUtil.isNotEmpty(messagePo.getId())) {
            messagePo.setUpdateBy("admin");
            messagePo.setUpdateTime(new Date());
        }
        this.saveOrUpdate(messagePo);
        result.setCode(200);
        return result;
    }

    @Override
    public MessageVo detail(String id) {
        MessagePo byId = this.getById(id);
        return BeanUtil.copyProperties(byId, MessageVo.class);
    }

    @Override
    public void send(List<MessageDto> messageDtoList) {
        if (CollUtil.isNotEmpty(messageDtoList)) {
            List<UserPo> userPoList= userService.list();
            if (CollUtil.isNotEmpty(userPoList)) {
                List<String> emailList = userPoList.stream().map(UserPo::getEmail).collect(Collectors.toList());
                messageDtoList.forEach(item->{
                    sendEmailUtil.sendMail(item.getTopic(),item.getMessageInfo(),emailList.toArray(new String[]{}));
                });
            }
            messageDtoList.forEach(item->{
                item.setSendStatue("已发送");
                item.setSendTime(new Date());
            });
            List<MessagePo> messagePos = BeanUtil.copyToList(messageDtoList, MessagePo.class, new CopyOptions());
            this.saveOrUpdateBatch(messagePos);
        }
    }
}




